package com.joke;

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.guohead.sdk.GuoheAdLayout;
import com.guohead.sdk.GuoheAdManager;
import com.joke.model.Constants;
import com.joke.model.Story;

public class JokeText extends Activity {
	
	private TextView title,content;
	private List<Story> mList;
	private int miCurID,miTotal;
	private Button upItem , nextItem;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content);

        /**guohe ad start**/
		LinearLayout.LayoutParams GuoheAdLayoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        LinearLayout footer = (LinearLayout)findViewById(R.id.adLayout);
        GuoheAdLayout adLayout = new GuoheAdLayout(JokeText.this);
        footer.addView(adLayout, GuoheAdLayoutParams);
        footer.invalidate();
        /**guohe ad end**/
        
        mList = Constants.gStoryList;
       
        Intent intent = this.getIntent();
        title = (TextView)this.findViewById(R.id.title);
        content = (TextView)this.findViewById(R.id.content);
        upItem = (Button)this.findViewById(R.id.upItem);
        nextItem = (Button)this.findViewById(R.id.nextItem);
        
        miCurID = intent.getIntExtra("jokeID",0);
        miTotal = mList.size();
        
        title.setText(mList.get(miCurID).getTitle());
        content.setText(mList.get(miCurID).getContent());
       
        upItem.setOnClickListener(beforeListener);
        nextItem.setOnClickListener(nextListener);
    }
	
	/**
	 * 监听下条按钮
	 */
	private OnClickListener nextListener = new OnClickListener(){
		@Override
		public void onClick(View view) {
			if(miCurID >= (miTotal-1)){
				miCurID = miTotal-1;
				Toast.makeText(JokeText.this, getString(R.string.noLastItem), Toast.LENGTH_SHORT).show();
			}else{
				miCurID++;
				title.setText(mList.get(miCurID).getTitle());
		        content.setText(mList.get(miCurID).getContent());
			}
		}
	};
	
	/**
	 * 监听上条按钮
	 */
	private OnClickListener beforeListener = new OnClickListener(){
		@Override
		public void onClick(View view) {
			if(miCurID <= 0){
				miCurID = 0;
				Toast.makeText(JokeText.this, getString(R.string.noHeadItem), Toast.LENGTH_SHORT).show();
			}else{
				miCurID--;
				title.setText(mList.get(miCurID).getTitle());
		        content.setText(mList.get(miCurID).getContent());
			}
		}
	};
	
	@Override
    protected void onDestroy() {
      super.onDestroy();
      GuoheAdManager.finish(this);
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, JokeText.this.getString(R.string.feedBack));
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
			case 0:
				/**发送邮件**/
			    Intent email = new Intent(android.content.Intent.ACTION_SEND);
			    email.setType("plain/text");
			    String[] emailReciver = new String[]{getString(R.string.feedBackEmailHu), getString(R.string.feedBackEmailQiu)};
			    String emailSubject = getString(R.string.feedBackSubject);
			    String emailBody = "";
			    /**设置邮件默认地址**/
			    email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);
			    /**设置邮件默认标题**/
			    email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailSubject);
			    /**设置要默认发送的内容**/
			    email.putExtra(android.content.Intent.EXTRA_TEXT, emailBody);
			    /**调用系统的邮件系统**/
			    startActivity(Intent.createChooser(email, getString(R.string.feedBackTips))); 
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}
}